<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="学号" prop="studentCode">
      <el-input v-model="dataForm.studentCode" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="学生姓名" prop="studentName">
      <el-input v-model="dataForm.studentName" placeholder="学生姓名"></el-input>
    </el-form-item>
    <el-form-item label="改卷老师名称" prop="teacherName">
      <el-input v-model="dataForm.teacherName" placeholder="所属班主任编号"></el-input>
    </el-form-item>
    <el-form-item label="考试编码" prop="examCode">
      <el-input v-model="dataForm.examCode" placeholder="考试编码"></el-input>
    </el-form-item>
    <el-form-item label="考试名称" prop="examName">
      <el-input v-model="dataForm.examName" placeholder="考试名称"></el-input>
    </el-form-item>
    <el-form-item label="考试进度" prop="examProgress">
    <div>
    <el-radio-group v-model="dataForm.examProgress">
      <el-radio-button v-model="dataForm.examProgress" label="未完成"></el-radio-button>
      <el-radio-button v-model="dataForm.examProgress" label="已完成"></el-radio-button>
    </el-radio-group>
  </div>
    </el-form-item>
    <el-form-item label="考试成绩" prop="examGrades">
      <el-input v-model="dataForm.examGrades" placeholder="考试成绩"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gmtCreate: '',
          createBy: '',
          gmtModified: '',
          lastModifiedBy: '',
          isDeleted: '',
          sortNo: '',
          version: '',
          studentCode: '',
          studentName: '',
          teacherName: '',
          examCode: '',
          examName: '',
          examProgress: '',
          examGrades: ''
        },
        dataRule: {
          gmtCreate: [
            { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '记录创建者不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
          ],
          lastModifiedBy: [
            { required: true, message: '记录最后更新人不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(1删除,0未删除)不能为空', trigger: 'blur' }
          ],
          sortNo: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
          studentCode: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          studentName: [
            { required: true, message: '学生姓名不能为空', trigger: 'blur' }
          ],
          examCode: [
            { required: true, message: '考试编码不能为空', trigger: 'blur' }
          ],
          examName: [
            { required: true, message: '考试名称不能为空', trigger: 'blur' }
          ],
          examProgress: [
            { required: true, message: '考试进度不能为空', trigger: 'blur' }
          ],
          examGrades: [
            { required: true, message: '考试成绩不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/student/tstudentexam/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gmtCreate = data.tStudentExam.gmtCreate
                this.dataForm.createBy = data.tStudentExam.createBy
                this.dataForm.gmtModified = data.tStudentExam.gmtModified
                this.dataForm.lastModifiedBy = data.tStudentExam.lastModifiedBy
                this.dataForm.isDeleted = data.tStudentExam.isDeleted
                this.dataForm.sortNo = data.tStudentExam.sortNo
                this.dataForm.version = data.tStudentExam.version
                this.dataForm.studentCode = data.tStudentExam.studentCode
                this.dataForm.studentName = data.tStudentExam.studentName
                this.dataForm.teacherName = data.tStudentExam.teacherName
                this.dataForm.examCode = data.tStudentExam.examCode
                this.dataForm.examName = data.tStudentExam.examName
                this.dataForm.examProgress = data.tStudentExam.examProgress
                this.dataForm.examGrades = data.tStudentExam.examGrades
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/student/tstudentexam/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gmtCreate': this.dataForm.gmtCreate,
                'createBy': this.dataForm.createBy,
                'gmtModified': this.dataForm.gmtModified,
                'lastModifiedBy': this.dataForm.lastModifiedBy,
                'isDeleted': this.dataForm.isDeleted,
                'sortNo': this.dataForm.sortNo,
                'version': this.dataForm.version,
                'studentCode': this.dataForm.studentCode,
                'studentName': this.dataForm.studentName,
                'teacherName': this.dataForm.teacherName,
                'examCode': this.dataForm.examCode,
                'examName': this.dataForm.examName,
                'examProgress': this.dataForm.examProgress,
                'examGrades': this.dataForm.examGrades
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
